package controller;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;

import org.activiti.engine.history.HistoricVariableInstance;
import org.activiti.engine.task.Task;
import org.junit.Assert;

import wfm.activiti.ActivitiManager;
import wfm.entities.Customer;
import wfm.entities.Ticket;
import wfm.entities.User;
import wfm.entities.WFInfo;
import wfm.query.QueryDao;
import wfm.util.email.Mail;
import wfm.util.email.MailAccounts;

import com.web.util.WebUtils;

@ManagedBean(name = "informRenterController")
@SessionScoped

public class InformRenterController {
	private Customer customer;  
	
	@ManagedProperty(value = "#{user}")
	private User user;
	
	@ManagedProperty(value = "#{wfInfo}")
	private WFInfo wfInfo;
	
	@ManagedProperty(value="renterInformation")
	private String renterInformation; 
	
	private List<Ticket> historicTickets;
	
	public InformRenterController(){
		
	}
	
	@PostConstruct
	public void init(){
		Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
		customer = ticket.getCustomer();
		
		historicTickets = new ArrayList<Ticket>();
		List<HistoricVariableInstance> instances = QueryDao.getAllHistoricVariableInstances("ticket");
		
		for(HistoricVariableInstance instance : instances){
			try{
				Ticket t = (Ticket)instance.getValue();
				
				if(t.getId() != ticket.getId() && t.getCustomer().getAddress().equals(ticket.getCustomer().getAddress()) ){
					historicTickets.add(t);
				}
			}catch (Throwable t){
//				System.out.println("not serializable - old case");
			}
		}

	}

	
	/* Getter n Setter */
	public Customer getCustomer() {
		return customer;
	}

	public void setCustomer(Customer customer) {
		this.customer = customer;
	}
	
	public String getRenterInformation() {
		return renterInformation;
	}

	public void setRenterInformation(String renterInformation) {
		this.renterInformation = renterInformation;
	}
	
	public String finishInformRenter(){
		String nextPage = "myTasks";
		String instanceId = wfInfo.getInstanceId();
		String taskId = wfInfo.getTaskId(); 
		
		//Ticket holen updaten und wieder speichern
		Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(instanceId, "ticket");
		ticket.setRenterInformation(this.renterInformation);
		QueryDao.saveVariableToProcessById(instanceId, "ticket", ticket);
		
		System.out.println("finishedInformRenter");

		// Retrieving Activiti engine
		ActivitiManager manager = ActivitiManager.getInstance();

		try {
			manager.getTaskRuntimeService().complete(taskId);

		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
		
		Task task=QueryDao.getNextTaskAndSetAssignee(instanceId, user);  //holt sich den nächsten Task zur Gruppe und setzt Asignee
		
		if(task!=null){
			wfInfo.setTaskId(task.getId());
			nextPage = QueryDao.getFormKey(taskId);
		}

		return WebUtils.redirect(nextPage);
	}
	
	public String finishInformRenterWithMail(){		
		//Ticket holen updaten und wieder speichern
		Ticket ticket=(Ticket)QueryDao.getVariableToProcessById(wfInfo.getInstanceId(), "ticket");
		
		//send mail 
		String recipient = "MarkusFriedl@gmx.at";
        String subject = "Your Ticket ("+ticket.getId()+") has been answered";
        String text = this.renterInformation; 
        
        if(ticket.getCustomer()!=null){
        	recipient=ticket.getCustomer().getMailAddress();
        }
 
        try {
			Mail.send(MailAccounts.GMX, recipient, subject, text);
		} catch (Exception e) {
			Assert.fail();
		}     

        System.out.println("Sended");
		
		
		//finish
		return finishInformRenter();
	}
	
	public String abortInformRenter(){
		return WebUtils.redirect("myTasks");
	}
	
	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}

	public WFInfo getWfInfo() {
		return wfInfo;
	}

	public void setWfInfo(WFInfo wfInfo) {
		this.wfInfo = wfInfo;
	}

	public List<Ticket> getHistoricTickets() {
		return historicTickets;
	}

	public void setHistoricTickets(List<Ticket> historicTickets) {
		this.historicTickets = historicTickets;
	}
}
